package api

import (
	"gitee.com/DengAnbang/Computation/src/dbops"
	"gitee.com/DengAnbang/Computation/src/bean"
	"path"
	"strings"
	"gitee.com/DengAnbang/Computation/src/code"
)

/**
	* showdoc
	* @catalog socket文档/模型相关
	* @title 获取节点上的模型列表
	* @description 获取节点上的模型列表的接口
	* @method socket type=401
	* @url /pc/model/modelBeanList
	* @param node 必选 string 节点的ID
	* @return {"code":0,"type":401,"message":"","debug_message":"","data":[{"model_id":"3D881B2B-E04C-4757-82F5-5D352B7F","node_id":"asdasdasd","path":"B3D/10013/3D881B2B-E04C-4757-82F5-5D352B7F/B2户型轻奢风格_V1.b3d","name":"B2户型轻奢风格_V1.b3d","created_id":"10013","versions":2},{"model_id":"589343EA-E58D-4C93-94CD-8FF5F37E","node_id":"asdasdasd","path":"B3D/10013/589343EA-E58D-4C93-94CD-8FF5F37E/B2户型轻奢风格_V1.b3d","name":"B2户型轻奢风格_V1.b3d","created_id":"10013","versions":1}]}
	* @return_param type int 请求类型
	* @remark 最后修改时间:2018.12.17 10.30
	* @number 5
	*/
func GetModelBeanListSocket(parameters map[string]string) error {
	nodeId := parameters["nodeId"]
	beans, err := dbops.SelectModelBean(nodeId)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(beans)
}

/**
	* showdoc
	* @catalog socket文档/模型相关
	* @title 获取节点上的最新模型
	* @description 获取节点上的最新模型接口
	* @method socket type=402
	* @url /pc/model/modelBean
	* @param node 必选 string 节点的ID
	* @return {"code":0,"type":402,"message":"","debug_message":"","data":{"model_id":"589343EA-E58D-4C93-94CD-8FF5F37E","node_id":"asdasdasd","path":"B3D/10013/589343EA-E58D-4C93-94CD-8FF5F37E/B2户型轻奢风格_V1.b3d","name":"B2户型轻奢风格_V1.b3d","created_id":"10013","versions":1}}
	* @return_param type int 请求类型
	* @remark 最后修改时间:2018.12.17 10.30
	* @number 5
	*/
func GetModelBeanSocket(parameters map[string]string) error {
	nodeId := parameters["nodeId"]
	beans, err := dbops.SelectModelBean(nodeId)
	if err != nil {
		return err
	}
	if len(beans) > 0 {
		return bean.NewSucceedMessage(beans[0])
	}
	return bean.NewSucceedMessage(nil)
}

/**
	* showdoc
	* @catalog socket文档/模型相关
	* @title 获取模型的结构树
	* @description 获取模型的结构树接口
	* @method socket type=402
	* @url /pc/model/modelBean
	* @param filePath 必选 string 节点上的模型的路径
	* @return {"code": 0,"type": 403,"message": "","debug_message": "","data": [{"id": "b3ecaf48-8adf-44bf-9f8a-5b27399728e3","pid": "2081e07f-26b0-4b47-85ff-f677f02bf15a","name": "衬砌_三心圆仰拱带喷混断面_左侧沟槽:1","flag": "2","tid": "672a77e57c7b578b"},{"id": "2081e07f-26b0-4b47-85ff-f677f02bf15a","pid": "","name": "衬砌_三心圆仰拱带喷混断面.iam","flag": "0","tid": ""}]}
	* @return_param type int 请求类型
	* @remark 最后修改时间:2019.01.14 10.30
	* @number 5
	*/
func SelectModelTreeSocket(parameters map[string]string) error {
	filePath := parameters["filePath"]
	if len(filePath) <= 0 {
		return bean.NewErrorMessage("路径不能为空").SetDeBugMessage(filePath)
	}
	fileSuffix := path.Ext(filePath)
	filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
	trees, err := dbops.SelectModelTree(code.FileRootPath + filenameOnly + ".db")
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(trees)
}

/**
	* showdoc
	* @catalog socket文档/模型相关
	* @title 查询模型树的结构的附加属性
	* @description 查询模型树的结构的附加属性接口
	* @method socket type=403
	* @url /pc/model/selectModelNodeAddition
	* @param filePath 必选 string 节点上的模型的路径
	* @param id 必选 string 节点上的模型的节点id
	* @return {"code":0,"type":404,"message":"","debug_message":"","data":[{"id": "3e414df9-3967-4974-b825-f66ae7d8fdd4","name": "Is3dElement","value": "True","unit": "","property_group": "共享单元","description": ""},{"id": "3e414df9-3967-4974-b825-f66ae7d8fdd4","name": "Is3dElement","value": "True","unit": "","property_group": "共享单元","description": ""}]}
	* @return_param type int 请求类型
	* @remark 最后修改时间:2019.01.14 10.30
	* @number 5
	*/
func SelectModelNodeAdditionSocket(parameters map[string]string) error {
	filePath := parameters["filePath"]
	id := parameters["id"]
	if len(filePath) <= 0 {
		return bean.NewErrorMessage("路径不能为空").SetDeBugMessage(filePath)
	}
	fileSuffix := path.Ext(filePath)
	filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
	additions, err := dbops.SelectModelNodeAddition(code.FileRootPath+filenameOnly+".db", id)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(additions)
}
